package com.getpebble.android.framework.protocol.inbound;

import com.getpebble.android.framework.protocol.inbound.PblInboundSystemMessage.Type;

public enum FirmwareUpdateResponseType {
    FIRMWARE_UPDATE_STOPPED((byte) 0),
    FIRMWARE_UPDATE_RUNNING((byte) 1),
    FIRMWARE_UPDATE_CANCELLED((byte) 2),
    UNKNOWN((byte) -1);
    
    private byte mCode;

    private FirmwareUpdateResponseType(byte code) {
        this.mCode = code;
    }

    public byte getCode() {
        return this.mCode;
    }

    public static FirmwareUpdateResponseType fromCode(byte code) {
        for (FirmwareUpdateResponseType response : values()) {
            if (response.getCode() == code) {
                return response;
            }
        }
        return UNKNOWN;
    }

    public static FirmwareUpdateResponseType from(PblInboundSystemMessage message) throws IllegalArgumentException {
        if (message.getType() == null || message.getType() != Type.FIRMWARE_UPDATE_START_ACK) {
            throw new IllegalArgumentException("Firmware update start cannot be created for system message of type: " + message.getType());
        }
        byte[] typeData = message.getTypeData();
        if (typeData == null) {
            throw new IllegalArgumentException("Firmware update response expected non-null type data");
        } else if (typeData.length == 1) {
            return fromCode(typeData[0]);
        } else {
            throw new IllegalArgumentException("Firmware update response expected length 1; actual length: " + typeData.length);
        }
    }
}
